/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tubes1sister.peerConnection.clientAsServer;

import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Muhammad Anis
 */
public class clientAsServer implements Runnable{
    public int port; //port where the tracker is listening
    static int maxConnection = 100;
    public int currentConnection = 0;
    public boolean isFinished = false;
    public String rootDirectory;
    //konstruktor

    public clientAsServer(int port,String rootDirectory){
        this.port = port;
        this.rootDirectory = rootDirectory;
    }
    //run
    public  void run (){
        
        try{
            ServerSocket listener = new ServerSocket(port);
            Socket server;
            while(currentConnection++ < maxConnection && !isFinished){
                System.out.println("Menunggu Client client As Server");
                System.out.println("Koneksi ke: " + currentConnection);                
                server = listener.accept();
                clientServerHandler cs = new clientServerHandler(server,rootDirectory);
                Thread t = new Thread(cs);
                t.start();
                //tracker = new TrackerHandler(server);
                //Thread t = new Thread(tracker);
                //t.start();

            }

        }
        catch(Exception e){

        }
    }

    public void finished(){
        this.isFinished = true;
    }

    public void startServer() {
        Thread t = new Thread(this);
        t.start();
    }

   
}
